import {Component, OnDestroy, OnInit} from "@angular/core";
import {Router} from "@angular/router";
import {CommService} from "../comm-service";
import {MyhttpService} from "../myhttp.service";
import {LocalStorageService} from "../local-storage-service";

@Component({
  template: `
    <div>
      <div>
        <topH></topH>
        <div class="collapse collapse in" id="content2">
          <div class="collapse navbar-collapse mypageheader" id="defaultNavbar1">
            <div class="row">
              <div class="left"></div>
              <div class="middle">
                <div class="hor-menu" style="margin: 0px auto;width:350px" [style.width]="ajustw">
                  <ul class="nav navbar-nav" style="font-size:14px;" *ngIf="topmenus.length">
                    <li *ngFor="let m of topmenus">
                      <a [class.tmactive]="m.isSelect" (click)="clickTM(m)">{{m.name}}</a>
                    </li>
                  </ul>
                </div>
              </div>
              <div class="right">
                <hkscc></hkscc>
              </div>
            </div>
          </div>
        </div>
      </div>
      <!--top-->
      <div class="clearfix"></div>
      <div>
        <router-outlet></router-outlet>
      </div>
      <!--slider-->
    </div>
  `,
  styles: [
    "#content2{background-color: #0050C3}",
    '#content2 ul li a.tmactive{color: #FFFFFF;background-color: #3598DC}',
    '#content2 ul li a{color: #b4bcc8;}',
    '#content2 ul li a:link{background: none}',
    '#content2 ul li a:visited{background: none}',
    '#content2 ul li a:hover{background-color: #3598DC;color: #FFFFFF}',
    ".left{width: 15%;float: left;height: 50px}",
    ".middle{width: 70%;float: left}",
    ".right{width: 15%;float: left}"
  ]
})
export class WbComponent implements OnInit, OnDestroy {
  ajustw: string = "0px";
  topmenus: any[] = [];

  getTopmenus(): void {
    const aestr_mtmenus = this.localstorage.get("mtmenus");
    if (!aestr_mtmenus) {
      return
    }
    const dastr_mtmenus = this.commservice.daesString(aestr_mtmenus);
    const resp = JSON.parse(dastr_mtmenus);
    const handtms = this.handtmenus(resp);
    this.topmenus = handtms;
    this.ajustw = handtms.length * 130 + "px";
    this.setCurentMenu();
  }

  /**
   * 排一下一级菜单的顺序
   * @param {any[]} r
   * @returns {any[]}
   */
  handtmenus(r: any[]): any[] {
    const topmenus = ["ywsbyw"];
    const retarr: any[] = [];
    for (let i = 0; i < topmenus.length; i++) {
      r.forEach(function (item) {
        if (topmenus[i] == item.url) {
          retarr.push({
            name: item.name,
            url: item.url,
            id: item.id,
            isSelect: false,
          });
        }
      });
    }
    return retarr;
  }

  /**
   * 处理跳转/刷新  一级菜单的选中焦点
   */
  setCurentMenu(): void {
    this.topmenus.forEach(function (value, index, array) {
      value.isSelect = false;
    });
    const topmenusUrls = this.topmenus.map((item) => {
      return item.url;
    });
    const index = this.routerFirstInurl(topmenusUrls);
    if (index != -1) {
      if (this.topmenus[index]) {
        this.topmenus[index]["isSelect"] = true;
        this.localstorage.set("tmenuid", this.topmenus[index].id);
      }
    } else {
      if (!topmenusUrls.length) {
        this.commservice.myswal("warning", "用户没有一个一级菜单", "", "ts", null);
      } else {
        this.clickTM(this.topmenus[0]);
      }
    }
  }


  clickTM(o: any): void {
    if (o.isSelect) {
      return;
    }
    this.router.navigate(["sjgzxin.jcsj", o.url]);
    this.localstorage.set("tmenuid", o.id);
    this.topmenus.forEach(function (value, index, array) {
      value.isSelect = false;
      if (o.url == value.url) {
        value.isSelect = true;
      }
    });
  }

  routerFirstInurl(topmenusUrls: any): number {
    let index: number = -1;
    const location_hash = this.router.url;
    const menus = location_hash.split("/");
    const menus2 = menus[2];
    const nowtargeturl = menus2.split(".")[0];
    index = topmenusUrls.indexOf(nowtargeturl);
    return index;
  }

  constructor(private commservice: CommService,
              private itservice: MyhttpService,
              private localstorage: LocalStorageService,
              private router: Router) {

  }

  ngOnInit() {
    this.getTopmenus();
  }

  ngOnDestroy() {

  }

}
